﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AutosysLib.toolWrapper
{
	partial class JilFile
	{
        /// <summary>
        /// in a Jil file, there are BOX jobs, commands, and FileWatcher jobs.
        /// </summary>
        public enum JobType
        {
            BOX,
            CMD,
            FW
        }

        public JobType? TryParseJobType(string jobTypeStr)
        {
            switch (jobTypeStr.ToUpper())
            {
                case "C":
                case "CMD": return JobType.CMD;
                case "B":
                case "BOX": return JobType.BOX;
                case "F":
                case "FW": return JobType.FW;
                default:
                    return null;
            }
        }

        public class Job
        {
            /// <summary>jobs can reference precondition jobs from other instances</summary>
            public bool isImplied;

            // fields parsed from input stream:
            public string jobName;

            /// <summary>jobs can reference precondition jobs from other instances</summary>
            public string autosysInstance;

            /// <summary>job type - BOX, command (CMD) or FileWatch (FW)</summary>
            public JobType jobType;
            public string parent_box_name;
            public string preconditionLine;
            public string command, machine;
            public string description;
            public string owner;
            public string watch_file;
            public string std_out_file, std_err_file;
            public List<string> allLines = new List<string>();

            // fields assigned in Postprocess fct:
            public int lineNumber;
            public Job parentJob;
            public List<Job> childJobs = new List<Job>();
            public int depth;
            public int orderWithinParent;
            public List<Job> preconditionJobs = new List<Job>();

            public void addLine(string lineTrimmed, string leftPart_, string rightPart_)
            {
                allLines.Add(lineTrimmed);
                string leftPartL = leftPart_.Trim().ToLower();
                string rightPart = rightPart_.Trim();
                switch (leftPartL)
                {
                    case "": return;
                    case "box_name": this.parent_box_name = rightPart; break;
                    case "condition": this.preconditionLine = rightPart; break;
                    case "machine": this.preconditionLine = rightPart; break;
                    case "std_out_file": this.std_out_file = rightPart; break;
                    case "std_err_file": this.std_err_file = rightPart; break;
                }

            }

            public override string ToString()
            {
                string kid = (childJobs == null) ? "null kids" : childJobs.Count + " kids";
                return "{Job " + jobName + " " + jobType + " " + kid + "}";
            }
            public IEnumerable<string> EnumLines()
            {
                return allLines;
            }
        }
    }
}
